package com.ht.medicaltreatment.activity

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.view.View
import androidx.activity.viewModels
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils

import com.gym.baselib.base.BaseActivity
import com.gym.baselib.utils.CacheUtils
import com.hjq.http.EasyConfig
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.base.CacheKey
import com.ht.medicaltreatment.utils.NFCUtils
import com.ht.medicaltreatment.utils.ScanUtils
import com.ht.medicaltreatment.utils.UIDUitls
import com.ht.medicaltreatment.viewmodel.LoginViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_login_layout.cbNameLogin
import kotlinx.android.synthetic.main.activity_login_layout.cbScanLogin
import kotlinx.android.synthetic.main.activity_login_layout.etAccount
import kotlinx.android.synthetic.main.activity_login_layout.etPSW
import kotlinx.android.synthetic.main.activity_login_layout.etPort
import kotlinx.android.synthetic.main.activity_login_layout.groupUser
import kotlinx.android.synthetic.main.activity_login_layout.imgScall
import kotlinx.android.synthetic.main.activity_login_layout.login
import kotlinx.android.synthetic.main.activity_login_layout.tvIP

class LoginActivity : BaseActivity() {
    private val mViewModel by viewModels<LoginViewModel>()
    private var isNFC = false

    override fun getLayoutId() = R.layout.activity_login_layout

    override fun init() {

        //StatusBarCompat.setTranslucent(window, true)
        val ip = CacheUtils.getStringCache(CacheKey.IP)
        val port = CacheUtils.getStringCache(CacheKey.PORT)
        if(port.isNotEmpty()){
            etPort.setText(port)
        }
        if(ip.isNotEmpty()){
            tvIP.setText(ip)
        }else{
            CacheUtils.save(CacheKey.IP,"59.110.162.243")
            CacheUtils.save(CacheKey.PORT,"8080")
        }
        isNFC = packageManager.hasSystemFeature(PackageManager.FEATURE_NFC)
        if(isNFC && Build.MODEL != "V9100"){
            NFCUtils(this)
        }else{
            ScanUtils.init()
        }
        //回显账号和密码
        val name = CacheUtils.getStringCache(CacheKey.USERNAME)
        val psw = CacheUtils.getStringCache(CacheKey.PASSWORD)
        //etAccount.setText(name)
        //etPSW.setText(psw)
        login.setOnClickListener {
            val ip = tvIP.text.toString()
            val port = etPort.text.toString()
            if(ip.isNotEmpty() && port.isNotEmpty()){
                CacheUtils.save(CacheKey.IP,ip)
                CacheUtils.save(CacheKey.PORT,port)
            }else{
                showToast("请输入ip地址和端口号")
                return@setOnClickListener
            }
            mViewModel.checkPermission(this,{
                val account = etAccount.text.toString()
                val psw = etPSW.text.toString()
                if(account == "" || psw == ""){
                    showToast("请填写账号/密码")
                    return@checkPermission
                }
                showProgress()
                mViewModel.login(account,psw,{
                    dismissProgress()
                    CacheUtils.save(CacheKey.USERNAME,account)
                    CacheUtils.save(CacheKey.PASSWORD,psw)
                    CacheUtils.save(CacheKey.MAIN_NAME,it.chineseName)
                    CacheUtils.save(CacheKey.NAME,it.name)
                    CacheUtils.save(CacheKey.TOKEN,it.token)
                    EasyConfig.getInstance().addHeader("Mobile-Custom-Token", it.token)
                    val intent = Intent(this,MainActivity::class.java)
                    intent.putExtra("name",it.name)
                    startActivity(intent)
                    finish()
                },{
                    dismissProgress()
                    showToast(it)
                })
            })
        }

        //账号登录
        cbNameLogin.setOnClickListener {
            if(cbNameLogin.isChecked){
                groupUser.visibility = View.VISIBLE
                imgScall.visibility = View.GONE
                cbScanLogin.isChecked = false
            }else{
                cbNameLogin.isChecked = true
            }
        }

        //扫码登录
        cbScanLogin.setOnClickListener {
            if(Build.MODEL == "V9100" || isNFC){
                if(cbScanLogin.isChecked){
                    groupUser.visibility = View.GONE
                    imgScall.visibility = View.VISIBLE
                    cbNameLogin.isChecked = false
                }else{
                    cbScanLogin.isChecked = true
                }
            }else{
                cbScanLogin.isChecked = false
                ToastUtils.showLong("该功能需要使用高频设备或者具有NFC功能的手机")
            }
        }

        imgScall.setOnClickListener {
            ScanUtils.open {uid->
                val ip = tvIP.text.toString()
                if(ip != null){
                    CacheUtils.save(CacheKey.IP,ip)
                    showProgress()
                    mViewModel.login(uid,{
                        dismissProgress()
                        ScanUtils.close()
                        CacheUtils.save(CacheKey.SCAN_UID,uid)
                        CacheUtils.save(CacheKey.MAIN_NAME,it.name)
                        CacheUtils.save(CacheKey.NAME,it.name)

                        CacheUtils.save(CacheKey.TOKEN,it.token)
                        EasyConfig.getInstance().addHeader("Mobile-Custom-Token", it.token)
                        EasyConfig.getInstance().addHeader("ttoekn", "322123123")
                        LogUtils.i("设置了")
                        val intent = Intent(this,MainActivity::class.java)
                        intent.putExtra("name",it.name)
                        startActivity(intent)
                        finish()
                    },{
                        showToast(it)
                        dismissProgress()
                    })
                }else{
                    showToast("请输入ip地址")
                }
            }
        }
    }


    override fun onResume() {
        super.onResume()
        if(isNFC && "V9100" != Build.MODEL){
            LogUtils.i("开启NFC")
            NFCUtils.mNfcAdapter.enableForegroundDispatch(this,NFCUtils.mPendingIntent,NFCUtils.mIntentFilter, NFCUtils.mTechList)
        }
    }

    override fun onPause() {
        super.onPause()
        if(isNFC && "V9100" != Build.MODEL){
            NFCUtils.mNfcAdapter.disableForegroundDispatch(this)
        }
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        if(cbScanLogin.isChecked && isNFC && "V9100" != Build.MODEL){
            var uid: String = NFCUtils.readNFCId(intent)
            uid = UIDUitls.sort(uid)
            val ip = tvIP.text.toString()
            if(ip != null) {
                CacheUtils.save(CacheKey.IP, ip)
                mViewModel.login(uid!!,{
                    dismissProgress()
                    CacheUtils.save(CacheKey.SCAN_UID,uid)
                    CacheUtils.save(CacheKey.MAIN_NAME,it.chineseName)
                    CacheUtils.save(CacheKey.NAME,it.name)
                    //startActivity(MainActivity::class.java)
                    val intent = Intent(this,MainActivity::class.java)
                    intent.putExtra("name",it.name)
                    startActivity(intent)
                    finish()
                },{
                    showToast(it)
                    dismissProgress()
                })
            }else{
                showToast("请输入IP")
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ScanUtils.onDestroy()
    }
}